package com.krain.corelibrary.util;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * Class description
 * sharedPreferences帮助类
 *
 * @author yamin
 * @date 2015/8/6 下午 03:07
 */
public class SpUtil {

    private static SharedPreferences sharedPreferences;
    private static final String SP_KEY = "ananzu.sp";
    private static SpUtil spUtil;
    private static Context mContext;

    private SpUtil() {
    }

    public static synchronized SpUtil getInstance(Context context){
        if (spUtil == null) spUtil = new SpUtil();
        mContext = context;
        return spUtil;
    }

//    public static void initUtil(Context context) {
//        SpUtil.mContext = context;
//    }

     SharedPreferences getsp() {
        if (sharedPreferences == null)
            sharedPreferences = mContext.getSharedPreferences(SP_KEY, Context.MODE_PRIVATE);
        return sharedPreferences;
    }

    public  void putString(String key, String value) {
        SharedPreferences.Editor editor = getsp().edit();
        editor.putString(key, value);
        editor.commit();
    }

    public  void putInt(String key, int value) {
        SharedPreferences.Editor editor = getsp().edit();
        editor.putInt(key, value);
        editor.commit();
    }

    public  void putBoolean(String key, boolean value) {
        SharedPreferences.Editor editor = getsp().edit();
        editor.putBoolean(key, value);
        editor.commit();
    }

    public  String getString(String key, String defaultValue) {
        return getsp().getString(key, defaultValue);
    }

    public  Boolean getBoolean(String key, boolean defaultValue) {
        return getsp().getBoolean(key, defaultValue);
    }

    public  Integer getInt(String key, int defaultValue) {
        return getsp().getInt(key, defaultValue);
    }


}
